package model

import (
	"tushare/model/mysql"
)

type Word struct {
	Id            int64  `json:"id"`
	Name          string `json:"name"`
	LevelFour     int64  `json:"level_four"`
	LevelSix      int64  `json:"level_six"`
	LevelGraduate int64  `json:"level_graduate"`
	LevelToefl    int64  `json:"level_toefl"`
	LevelIelts    int64  `json:"level_ielts"`
	LevelGre      int64  `json:"level_gre"`
}

func WordCreate(w Word) {
	mysql.DB.Table("word").Create(&w)
}

func WordInfoByName(name string) Word {
	w := Word{}
	mysql.DB.Table("word").Where("name = ?", name).Scan(&w)
	return w
}

func WordUpdate(id int64, w Word) {
	mysql.DB.Table("word").Where("id = ?", id).Update(&w)
}

func WordList(page, pageSize int64) []Word {
	page = page
	pageSize = pageSize
	offset := (page - 1) * pageSize

	w := []Word{}
	mysql.DB.Table("word").Limit(pageSize).Offset(offset).Find(&w)
	return w
}

func WordDelById(id int64) {
	w := Word{}
	w.Id = id
	mysql.DB.Table("word").Delete(w)
}

func WordListPage(page, pageSize int64, wordType int64) []Word {
	page = page
	pageSize = pageSize
	offset := (page - 1) * pageSize

	w := []Word{}
	table := mysql.DB.Table("word")
	if wordType == 4 {
		table = table.Where("level_four = 1")
	} else if wordType == 6 {
		table = table.Where("level_six = 1")
	}
	table = table.Limit(pageSize).Offset(offset).Find(&w)

	return w
}
